

import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/values/text_styles.dart';
import 'package:gczn_hd/app/core/widget/gczn_page.dart';
import 'package:gczn_hd/app/modules/eam/spotcheck/controllers/spotcheck_controller.dart';
import 'package:get/get.dart';

import '../../../../core/values/app_colors.dart';
import '../../../../core/values/app_values.dart';

class SpotLog extends StatelessWidget{
  final SpotcheckController controller = Get.put(SpotcheckController());

  @override
  Widget build(BuildContext context) {
    return GcznPage(
      easyController: controller.gcznLogController.value,
      onRefresh: () async {
        await controller.onRefresh();
      },
      onLoadNextPage: () async {
        await controller.onLoadNextPage();
      },
      child: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.only(top: AppValues.size40.h),
          padding: EdgeInsets.only(left: AppValues.size40.w, right: AppValues.size40.w),
          child: Wrap(
            spacing: AppValues.size36, // 水平间距
            runSpacing: AppValues.size36, // 垂直间距
            children: [1,2,3,4,5,6,7].map((item) =>  _buildItem(context, item)).toList(),
          ),
        )
      ),
    );
  }


  Widget _buildItem(BuildContext context, int idx){

    return GestureDetector(
      onTap: (){
        // TODO
      },
      child: Container(
          width: (MediaQuery.of(context).size.width - 3 * AppValues.size36 - AppValues.size20) / 3, // 计算每个子项的宽度
          decoration: BoxDecoration(
            border: Border.all(
              color: const Color.fromRGBO(39, 65, 80, 1),
              width: 1.0,
            ),
            borderRadius: BorderRadius.circular(AppValues.margin_12.r),
            color: AppColors.processBtn,
            boxShadow: const [pressShadow],
          ),
          clipBehavior: Clip.hardEdge,
          child: Column(
            children: [
              _buildTitleBar(idx),
              _buildOrderMsg(),
            ],
          )
      ),
    );
  }



  Widget _buildOrderMsg(){
    List<Map<String, String>> list = [
      {"name": "点检人员", "value": "冠生"},
      {"name": "点检时间", "value": "2024-2-28 19:42:51"},
      {"name": "点检结果", "value": "检查9项，8项正常"},
    ];
    const double labelWidth = 130.0;

    return Container(
      padding: EdgeInsets.only(left: AppValues.size37.w, right: AppValues.size37.w, top: AppValues.size30.h, bottom: AppValues.size30.h),
      child: Column(
        children: list.map((item) => Container(
          margin: const EdgeInsets.only(bottom: 7),
          child: Row(
            children: [
              SizedBox(
                width: labelWidth.w,
                child: Text("${item['name']}: ", style: textStyle20Opa8,),
              ),
              Expanded(child: Text(item['value']!, style: textStyle, maxLines: 1, overflow: TextOverflow.ellipsis,))
            ],
          ),
        )).toList(),
      ),
    );
  }


  Widget _buildTitleBar(int idx){
    const double barHeight = 73.0;

    return Container(
      height: barHeight.h,
      padding: EdgeInsets.only(left: AppValues.size37.w, right: AppValues.size37.w),
      color: AppColors.titlebarBackground,
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: Text("注塑机A08", style: submitButtonText,),
          ),
          Text(idx % 2 > 0 ? '待提交' : '已完成', style: toggleStyle(idx % 2 > 0 ? '待提交' : '已完成'),)
        ],
      ),
    );
  }

  TextStyle toggleStyle(String text){
    if( text == '待提交' ) {
      return waitTextStyle;
    } else if( text == '待审核' ) {
      return fialTextStyle;
    } else {
      return  successTextStyle;
    }
  }

}